package cn.dengta.webapp.user.entity;

import java.util.Date;
import javax.annotation.Nullable;

import cn.dengta.common.model.Status;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.entity.I18nContent;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.*;
import org.apache.commons.lang3.StringUtils;

/**
 * app版本
 *
 * @author lgy
 * @since 2022-03-02
 */
@Setter
@Getter
public class AppInfo extends TimeBasedEntity {

	public static final String MAIN = "main";
	public static final String MAIN_HOME = "mainHome";
	/** 分包 */
	private WesKit wesKit;

	/** APP客户端类型：2=Android；3=iOS {@link cn.dengta.common.web.Client} */
	private String client;
	/** 版本号 */
	private String version;
	/** 版本号（整数标识） */
	private Integer versionInt;
	/** 渠道 */
	private String channel;
	/** Logo */
	private String logo;
	/** 版本描述 */
	@TableField(value = "`desc`")
	private String desc;
	/** 下载链接 */
	private String downloadUrl;
	/** 是否强制更新 */
	private Integer isMandatory;
	/** 启用状态 */
	private Integer status;

	protected transient I18nContent resolvedLanguageCfg;

	public AppInfo init(AppInfo input, Date now) {
		this.client = X.expectNotNull(input.client, client);
		this.version = X.expectNotNull(input.version, version);
		this.versionInt = X.expectNotNull(input.versionInt, versionInt);
		this.logo = X.expectNotEmpty(input.logo, logo);
		this.desc = X.expectNotNull(input.desc, desc);
		this.channel = X.expectNotNull(input.channel, channel, MAIN);
		this.downloadUrl = X.expectNotEmpty(input.downloadUrl, downloadUrl);
		this.isMandatory = X.expectNotNull(input.isMandatory, isMandatory, 0);
		this.status = X.expectNotNull(input.status, status, 0);
		this.wesKit = input.getWesKit();
		super.initTime(now);
		return this;
	}

	@Nullable
	public I18nContent getResolvedLanguageCfg() {
		I18nContent cfg = resolvedLanguageCfg;
		if (cfg == null) {
			resolvedLanguageCfg = cfg = StringUtil.isEmpty(desc) ? null : I18nContent.deserialize(desc);
		}
		return X.castType(cfg);
	}

	public static String defaultChannel(boolean inChina) {
		return inChina ? MAIN_HOME : MAIN;
	}

	public void updateDesc(I18nContent config) {
		this.desc = JSONUtil.serialize(this.resolvedLanguageCfg = config);
	}

	public I18nContent updateResolvedLanguageCfg(String config) {
		this.resolvedLanguageCfg = I18nContent.deserialize(this.desc = config);
		return getResolvedLanguageCfg();
	}

	public static String getChannel(String appId) {
		// App-Id："dt_intl_front/iOS/1.1.0/main"
		return StringUtils.substringAfterLast(appId, "/");
	}

	public boolean isPass() {
		return Status.asResult(status);
	}

	public boolean isWaitVerify() {
		return Status.NO.eq(status);
	}

	public void setId(Long id) {
		this.id = id;
	}

	public static final String WES_KIT = "wes_kit";
	public static final String CLIENT = "client";
	public static final String CHANNEL = "channel";
	public static final String VERSION = "version";
	public static final String VERSION_INT = "version_int";
	public static final String LOGO = "logo";
	public static final String DESC = "desc";
	public static final String DOWNLOAD_URL = "download_url";
	public static final String IS_MANDATORY = "is_mandatory";
	public static final String STATUS = "status";

}
